/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package neo.workforce.dao;

import java.io.Serializable;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 *
 * @author krisada
 */
public class CommonDAO implements Serializable {
    private static final long serialVersionUID = -5122650303469499705L;

    private HibernateTemplate hibernateTemplate;
    private Logger log = Logger.getLogger(this.getClass());

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public Object find(Class clazz, long id) {
        return this.hibernateTemplate.get(clazz, new Long(id));
    }

    public Long save(Object object) {
        Long id = null;
        try {
            id = (Long) hibernateTemplate.save(object);
        } catch (Exception ex) {
            log.info("Save Object Common Error", ex);
        }
        return id;
    }

    public boolean update(Object object) {
        try {
            hibernateTemplate.update(object);
            return true;
        } catch (Exception ex) {
            log.info("Update Object Common Error", ex);
            return false;
        }
    }

    public boolean remove(Class clazz, long id) {
        try {
            Object object = this.find(clazz, id);
            hibernateTemplate.delete(object);
            return true;
        } catch (Exception ex) {
            log.info("Remove Object Common Error", ex);
            return false;
        }
    }
}
